package com.apes.autoparts.searcher.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.searcher.model.EpcPartCodeParse;
import com.apes.autoparts.searcher.model.EpcPartNumberParse;
import com.apes.autoparts.searcher.repository.AutopartsDao;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Service("epcPartNumberParseService")
public class EpcPartNumberParseService  extends DomainService {


    @Autowired
    AutopartsDao autopartsDao;

    /***
     * 功能 审核
     * @param request
     * @return
     * @throws RuntimeException
     */
    public JSONObject approve(SimpleRequest request) throws RuntimeException {

        // 转化为类
        EpcPartNumberParse epcPartNumberParse = request.getO(EpcPartNumberParse.class);
        Map<String, Set<String>> uniquePartNumberParse =  epcPartNumberParse.uniquePartNumberParse();
        uniquePartNumberParse.keySet().stream().forEach(partNumber -> {
            parseCore(uniquePartNumberParse.get(partNumber), epcPartNumberParse.getEpcVersion(), partNumber, epcPartNumberParse.getApproveId(), 0);
        });
        return request.getJO();
    }


    /***
     * epcPartCodeParse 编译
     */

    public JSONObject parse(SimpleRequest request) {
        EpcPartCodeParse epcPartCodeParse = request.getO(EpcPartCodeParse.class);
        parseCore(epcPartCodeParse.formatParseData(), epcPartCodeParse.getEpcVersion(), epcPartCodeParse.getPartCode(), epcPartCodeParse.get_updater(), 1);
        return request.getJO();
    }


    /***
     * 公共方法
     */
    private void parseCore(Set<String> pjmcArray, String epcVersion, String partNumber, Map person, int parseFlag) {
        // 删除原始的数据
        autopartsDao.deleteParseData(new JSONObject(MapUtil.mapper("epcVersion",epcVersion,
                "partNumber", partNumber)));

        insertParseResult(pjmcArray, epcVersion, partNumber, person);

        clearParseResult(pjmcArray, epcVersion, partNumber, person, parseFlag);
    }


    /***
     * 插入数据
     */
    private void insertParseResult(Set<String> pjmcArray, String epcVersion, String partNumber, Map person) {
        pjmcArray.stream().forEach(str -> {
            String[] strings = str.split(":");

            // 查询配件数据是否存在
            JSONObject pjmc = autopartsDao.queryPJMC(new JSONObject(MapUtil.mapper("pjmcid", strings[0])));

            // 写入新的数据
            autopartsDao.insertParseData(new JSONObject(MapUtil.mapper(
                    "EPC_BBMC", epcVersion,
                    "PARTNUMBER", partNumber,
                    "PJMCID", strings[0],
                    "PJMC", pjmc.getString("PJMC"),
                    "LRR", person.get("name"),
                    "TS01", DateUtil.format(new Date(), "yyyyMMdd.HHmmss.SSS"), "BYBJ", 1)));
        });
    }

    /***
     * 清空数据
     */
    private void clearParseResult(Set<String> pjmcArray, String epcVersion, String partNumber, Map person, int parseFlag) {
        // 对未做解析的数据插入一条空配件名称的数据
        if( pjmcArray.size() == 0 ) {
            // 先删除再写入
            autopartsDao.deleteParseData(new JSONObject(MapUtil.mapper("epcVersion", epcVersion,
                    "partNumber", partNumber)));
            autopartsDao.insertParseData(new JSONObject(MapUtil.mapper(
                    "EPC_BBMC", epcVersion,
                    "PARTNUMBER", partNumber,
                    "PJMCID", null,
                    "PJMC", null,
                    "LRR", person.get("name"), "TS01",
                    DateUtil.format(new Date(), "yyyyMMdd.HHmmss.SSS"), "BYBJ", parseFlag)));
        }
    }

    /***
     * 配件解析代办纠错删除所有已编译的数据，并插入一条空数据
     */
    public void deleteAll(SimpleRequest request) {
        EpcPartCodeParse epcPartCodeParse = request.getO(EpcPartCodeParse.class);
        clearParseResult(new HashSet<>(), epcPartCodeParse.getEpcVersion(), epcPartCodeParse.getPartCode(), MapUtil.mapper(
                "name", "编译中"
        ), 1);
    }
}
